﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JibberJabber.PartDesigners;

namespace JibberJabber.Controls
{
    public class SpeakersListComboBox : ComboBox
    {
        public SpeakersListComboBox()
            : base()
        {
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            if (Program.Root != null)
            {
                Program.Speakers.SpeakersListUpdated += new SpeakersListUpdated(Speakers_SpeakersListUpdated);
                Speakers_SpeakersListUpdated(); // manual update at first
            }
        }

        private void Speakers_SpeakersListUpdated()
        {
            int ii = SelectedIndex;
            object si = SelectedItem;

            lock (Program.Speakers.Children)
            {
                Items.Clear();
                foreach (PartDesigner pd in Program.Speakers.Children)
                    Items.Add(pd.ToString());
                if (si != null && Items.Contains(si))
                    SelectedItem = si;
                else if (ii > -1 && Items.Count > ii)
                    SelectedIndex = ii;
            }
        }
    }
}
